package fr.uniscol.service.impl;

import fr.uniscol.dao.IEleveDao;
import fr.uniscol.dao.IOptionDao;
import fr.uniscol.model.BaseAnneeScolaire;
import fr.uniscol.model.Classe;
import fr.uniscol.model.Eleve;
import fr.uniscol.model.EtablissementModel;
import fr.uniscol.model.Option;
import fr.uniscol.model.Scolarite;
import fr.uniscol.service.IEleveService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("eleveService")
public class EleveServiceImpl implements IEleveService {

	@Autowired
	private IEleveDao eleveDao;
    
    @Autowired
    private IOptionDao optionDao;
	
	private Eleve findEleveByIdInList(long eleveId, List<Eleve> eleves){
		for(Eleve e : eleves){
			if(e.getId() == eleveId){
				return e;
			}
		}
		return null;
	}
	
	@Override
	public boolean saveEleve(Eleve eleve) {
		
		boolean retEleve = eleveDao.saveEleve(eleve);
		
		// Scolarites
		/*long scoId = 0;
		if(retEleve){
			for(Scolarite sco : eleve.getScolarites()){
				scoId = eleveDao.saveScolariteEleve(eleve, sco);
                
                if(){
                    
                }
			}
		}
		
		return retEleve && retScolarites;*/
        return retEleve;
	}
    
    @Override
    @Transactional(readOnly = false)
    public boolean saveScolariteEleve(Scolarite scolarite) {
        
        boolean optionsOk = true;
        
        long scolariteId = eleveDao.saveScolariteEleve(scolarite.getEleve(), scolarite);

        if(scolariteId != 0){
            
            if(scolarite.getId() != 0){
                optionDao.deleteAllOptionsOfScolarite(scolarite);
                
            }else{
                scolarite.setId(scolariteId);
            }
            
            for(Option o : scolarite.getOptions()){
                optionsOk &= optionDao.saveOption(scolarite, o);
            }
        }
        
        return scolariteId != 0 && optionsOk;
    }

	@Override
	public List<Eleve> getAllElevesOfEtablissement(BaseAnneeScolaire base) {
		List<Eleve> eleves = eleveDao.getAllEleves(base);
		
		List<Scolarite> scolarites = eleveDao.getAllScolaritesActuelles(base);
		Eleve e = null;
		for(Scolarite sco : scolarites){
			e = findEleveByIdInList(sco.getEleve().getId(), eleves);
			if(e != null){
				e.getScolarites().add(sco);
			}
		}
		return eleves;
	}

	@Override
	public List<Eleve> getAllElevesOfClasse(Classe classe) {
		return eleveDao.getAllElevesOfClasse(classe);
	}

	@Override
	public Eleve getEleveById(long eleveId) {
		return eleveDao.getEleveById(eleveId);
	}

	@Override
	public List<Scolarite> getScolaritesOfEleve(Eleve eleve) {
		
        List<Scolarite> scolarites = eleveDao.getAllScolaritesOfEleve(eleve);
        
        for(Scolarite sco : scolarites){
            sco.setOptions(optionDao.getAllOptionsOfScolarite(sco));
        }
        
        return scolarites;
	}

    @Override
    @Transactional
    public boolean deleteScolariteEleve(Scolarite scolarite) {
        
        boolean deleteOptions = optionDao.deleteAllOptionsOfScolarite(scolarite);
        
        if(deleteOptions){
            return eleveDao.deleteScolariteEleve(scolarite);
        }else{
            return false;
        }
    }
}
